/**
 * 
 */
package javas.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author LionBule
 * 2011-4-23 07:49:12
 */
public class TestThreadPool {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TestThreadPool a = new TestThreadPool();
		Thread t = new Thread(a.new DaemonThread());
		t.start();
	}

	class DaemonThread implements Runnable{
		private boolean isRun = false;
		BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(10);
		ThreadPoolExecutor tpe = new ThreadPoolExecutor(3, 12, 1000, TimeUnit.MILLISECONDS, workQueue);
		ExecutorService executorService = Executors.newFixedThreadPool(10);
		
		@Override
		public void run() {
			while(true){
				if(isRun){
					try {
						System.out.println("**");
						if(tpe.isTerminated()){
							tpe.shutdown();
							System.out.println("�˳�!");
							return;
						}
						Thread.sleep(500);
					} catch (InterruptedException e) {
					}
				}else{
					isRun = true;
					for(int i=0;i<5;i++){
						tpe.execute(new DealThread());
					}
				}
			}
		}
		
	}
	
	class DealThread implements Runnable{

		@Override
		public void run() {
			int i = 10;
			while(true){
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
				}
				if(--i<0){
					System.out.println("quit.");
					return;
				}
			}
		}
		
	}
}
